package com.peppa.petshop.controller;

import com.peppa.petshop.entity.Customer;
import com.peppa.petshop.service.AddressService;
import com.peppa.petshop.service.IndentService;
import com.peppa.petshop.service.UserService;
import com.peppa.petshop.vo.IndentVO;
import com.peppa.petshop.vo.PetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author SongTiantian
 * @create 2021-06-22-22:24
 */

@Controller
@RequestMapping({"/user"})
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private IndentService indentService;
    @Autowired
    private AddressService addressService;
    @PostMapping("/register")
    public ModelAndView register(HttpServletRequest request,  @RequestParam("phone") String phone, @RequestParam("password") String password){
        System.out.println("我来注册了");
        ModelAndView modelAndView = new ModelAndView();
        Customer customer = userService.register(phone, password);
        HttpSession session = request.getSession();
        session.setAttribute("customerId",customer.getCustomerId());
        modelAndView.setViewName("user/main");
        return modelAndView;
    }


    @RequestMapping("/pets")
    @ResponseBody
    @CrossOrigin
    public List<PetVO> getPetList(){

        List<PetVO> petList = userService.getPetList();
        return petList;
    }

    @RequestMapping("/petsOrderBy")
    @ResponseBody
    @CrossOrigin
    public List<PetVO> getPetByL2H(String orderBy){
        System.out.println(orderBy);
        List<PetVO> petList = userService.getPetListOrderBy(orderBy);
        return petList;
    }

    @RequestMapping("/newPets")
    @ResponseBody
    @CrossOrigin
    public List<PetVO> getNewPetList(){
        List<PetVO> petList = userService.getNewPetList();
        return petList;
    }
    @RequestMapping("/hotPets")
    @ResponseBody
    @CrossOrigin
    public List<PetVO> getHotPetList(){
        List<PetVO> petList = userService.getHotPetList();
        return petList;
    }
    @RequestMapping("/lovePets")
    @ResponseBody
    @CrossOrigin
    public List<PetVO> getLovePetList(){
        System.out.println("爱心收养！！！！！");
        List<PetVO> petList = userService.getLovePetList();
        return petList;
    }


    @RequestMapping("/searchPets")
    @ResponseBody
    @CrossOrigin
    public List<PetVO> searchPets(String name){
        System.out.println("搜索");
        System.out.println(name);
        List<PetVO> petsList = userService.searchPets(name);
        return petsList;
    }

    @RequestMapping("/searchCategory")
    @ResponseBody
    @CrossOrigin
    public List<PetVO> searchCategory(Integer categoryId){
        System.out.println("搜索");
        System.out.println(categoryId);
        List<PetVO> petsList = userService.searchCategory(categoryId);
//        List<PetVO> petList = userService.getHotPetList();
        return petsList;
    }

    @RequestMapping("/myDeliveryIndent")
    @ResponseBody
    @CrossOrigin
    public List<IndentVO> getDeliveryIndent(HttpServletRequest request){
//        HttpSession session = request.getSession();
//        Integer customerId =(Integer) session.getAttribute("customerId");
        System.out.println(request.getParameter("customerId"));
        Integer customerId =new Integer("2");
        System.out.println("获取用户派送中订单列表");
        System.out.println(customerId);
        List<IndentVO> indentVOList = userService.getDeliveryIndents(customerId);
        return indentVOList;
    }
    @RequestMapping("/myIndent")
    @ResponseBody
    @CrossOrigin
    public List<IndentVO> getIndentList(HttpServletRequest request){
        System.out.println(request.getParameter("customerId"));
//        HttpSession session = request.getSession();
//        Integer customerId =(Integer) session.getAttribute("customerId");
        Integer customerId =new Integer("2");
        System.out.println("获取用户订单列表");
        System.out.println(customerId);
        List<IndentVO> indentVOList = userService.getIndentList(customerId);
        return indentVOList;
    }
    @RequestMapping("/mySuccessIndent")
    @ResponseBody
    @CrossOrigin
    public List<IndentVO> getSuccessIndent(HttpServletRequest request){
        System.out.println(request.getParameter("customerId"));
//        HttpSession session = request.getSession();
//        Integer customerId =(Integer) session.getAttribute("customerId");
        Integer customerId =new Integer("2");
        System.out.println("获取用户已完成订单列表");
        System.out.println(customerId);
        List<IndentVO> indentVOList = userService.getSuccessIndents(customerId);
        return indentVOList;
    }

    @GetMapping("/pet")
    public ModelAndView getPet(Integer goodId){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user/petdetails");
        return modelAndView;
    }

    @GetMapping("/petdetails")
    @ResponseBody
    public PetVO getPetDetails(Integer goodId){
        System.out.println("宠物详情页面数据");
        if (goodId ==null){
            System.out.println("我没带");
            return null;
        }
        PetVO petVO = userService.getPet(goodId);
        return petVO;
    }

    @RequestMapping("/buy")
    @ResponseBody
//    public String buyPet(HttpServletRequest request, List<Integer> goodId ,Integer indent_status
//    ,Integer address, BigDecimal totalPrice){
    public String buyPet(HttpServletRequest request, String goodId
            ,Integer address){
        System.out.println(request.getParameter("customerId"));
        System.out.println(goodId);
        String[] split = goodId.split(",");
        List<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add(new Integer(s));
        }
        System.out.println(address);
        HttpSession session = request.getSession();
//        Integer customerId = (Integer) session.getAttribute("customerId");
        Integer customerId = new Integer("2");
        System.out.println("购买宠物");
        String flag = indentService.insert(customerId,list,address);
        return "flag";
//        return flag;
    }
    @RequestMapping("/addAddress")
    @ResponseBody
    public String addAddress(HttpServletRequest request, String address,Integer isdefault,
                             String username,String phone){
        HttpSession session = request.getSession();
        System.out.println(request.getParameter("customerId"));

//        Integer customerId = (Integer) session.getAttribute("customerId");
        Integer customerId = new Integer("2");
        System.out.println("添加地址");
        String flag = addressService.insert(customerId,address,isdefault,username,phone);

        return flag;
    }

    @RequestMapping("/editPassword")
    @ResponseBody
    public String editPassword(HttpServletRequest request,String oldPass,String newPass){
        System.out.println(request.getParameter("customerId"));

//        Integer customerId = (Integer) session.getAttribute("customerId");
        Integer customerId = new Integer("2");
        System.out.println("修改密码");
        String flag = userService.editPassword(customerId,oldPass,newPass);

        return flag;
    }
    @RequestMapping("/sureIndent")
    @ResponseBody
    public String sureIndent(HttpServletRequest request,Integer indentId){
        System.out.println(request.getParameter("customerId"));
//        Integer customerId = (Integer) session.getAttribute("customerId");
        Integer customerId = new Integer("2");
        System.out.println("确认订单");
        String flag = indentService.sureIndent(indentId);

        return flag;
    }
    @RequestMapping("/returnPet")
    @ResponseBody
    public String returnPet(HttpServletRequest request,Integer indentId){
        System.out.println(request.getParameter("customerId"));
//        Integer customerId = (Integer) session.getAttribute("customerId");
        Integer customerId = new Integer("2");
        System.out.println("退货退款");
        String flag = indentService.returnPet(indentId);

        return flag;
    }
    @RequestMapping("/myInfo")
    @ResponseBody
    public Customer myInfo(HttpServletRequest request){
        System.out.println("获取用户信息"+request.getParameter("customerId"));
//        Integer customerId = new Integer(request.getParameter("customerId")) ;
        Integer customerId = new Integer("2") ;
        Customer customer= userService.getCustomer(customerId);

        return customer;
    }


}
